package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.example.demo.po.*;
import com.example.demo.service.*;
import com.example.demo.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.net.www.protocol.http.AuthCache;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/ajaxData")
public class AjaxController {

    @Autowired
    TBizOrderService tBizOrderService;
    @Autowired
    TBizAuctionService tBizAuctionService;
    @Autowired
    TBizArtService tBizArtService;
    @Autowired
    TBizAuctionArtService tBizAuctionArtService;
    @Autowired
    TBizCarouseService tBizCarouseService;
    @Autowired
    WxUserAddressService wxUserAddressService;
    @Autowired
    WxUserInfoService wxUserInfoService;
    @Autowired
    TBizAuctionRecordService arService;
    @Autowired
    TBizShoppingService tBizShoppingService;
    @Autowired
    TBizAuctionRemindService tBizAuctionRemindService;

    /**
     * 文件上传
     * @param file
     * @param response
     * @param request
     * @return
     */
    @RequestMapping(value = "/uploadFile")
    public String uploadFile(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        System.out.println(file.getOriginalFilename());
        try{
            String path = DateUtil.currentDateString().replaceAll("-", "") +"/";
            String newFileName = SeqFactory.getNewSequenceAlone() + ".jpg";
            String uploadPath =path+newFileName;
            //oss上传
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            byte [] byteArr = file.getBytes();
            oss.upload(ossClient,byteArr,uploadPath);
            String showPath = uploadPath;
            oss.shutDownClient(ossClient);
            String srcPath = oss.styleQuery(ossClient,showPath,"style/ys720");
            jsonObject.put("state", 200);
            jsonObject.put("fileName", file.getName());
            jsonObject.put("message", "操作成功");
            jsonObject.put("showPath", showPath);
            jsonObject.put("srcPath", srcPath);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("state", 500);
            jsonObject.put("message", "文件上传失败");
        }
        return jsonObject.toString();
    }

    /**
     * layui富文本图片上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadImage")
    public String uploadImage(MultipartFile file){
        JSONObject jsonObject = new JSONObject();
        System.out.println(file.getOriginalFilename());
        try{
            String path = DateUtil.currentDateString().replaceAll("-", "") +"/";
            String newFileName = SeqFactory.getNewSequenceAlone() + ".jpg";
            String uploadPath =path+newFileName;
            //oss上传
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            byte [] byteArr = file.getBytes();
            oss.upload(ossClient,byteArr,uploadPath);
            String showPath = uploadPath;
            oss.shutDownClient(ossClient);
            String srcPath = oss.styleQuery(ossClient,showPath,"style/ys320");
            jsonObject.put("code", 0);
            jsonObject.put("msg", "操作成功");
            Map<String,String> map = new HashMap<>();
            map.put("src",srcPath);
            map.put("title",file.getName());
            jsonObject.put("data", map);
//            jsonObject.put("fileName", file.getName());
//
//            jsonObject.put("showPath", showPath);
//            jsonObject.put("srcPath", srcPath);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code", 500);
            jsonObject.put("msg", "文件上传失败");
        }
        return jsonObject.toString();
    }



    /**
     * OSS图片读取
     * @param imgPath
     * @param type
     * @param response
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/photoQuery")
    public String photoQuery(String imgPath,String type,HttpServletResponse response, HttpServletRequest request) throws IOException {
        UploadALiYunOOS oos = new UploadALiYunOOS();
        JSONObject jsonObject = new JSONObject();
        if(imgPath!=null&&!"".equals(imgPath.trim())){
            imgPath=imgPath.replaceAll("//","/");
            int c =imgPath.substring(0, 1).indexOf("/");
            if(c!=-1)
                imgPath =imgPath.substring(1);
            OSSClient ossClient = oos.creatOSSClient();
            String url = null;
            if(type==null||"".equals(type))
                url = oos.styleQuery(ossClient,imgPath);
            else
                url = oos.styleQuery(ossClient,imgPath,type );

            oos.shutDownClient(ossClient);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",url);
        }else{
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return  jsonObject.toJSONString();
    }

    @RequestMapping(value = "/photoshow")
    public String photoshow(String imgPath,String type,HttpServletResponse response, HttpServletRequest request) throws IOException {
        UploadALiYunOOS oos = new UploadALiYunOOS();
        JSONObject jsonObject = new JSONObject();
        if(imgPath!=null&&!"".equals(imgPath.trim())){
            imgPath=imgPath.replaceAll("//","/");
            int c =imgPath.substring(0, 1).indexOf("/");
            if(c!=-1)
                imgPath =imgPath.substring(1);
            OSSClient ossClient = oos.creatOSSClient();
            String url = null;
            if(type==null||"".equals(type))
                url = oos.styleQuery(ossClient,imgPath);
            else
                url = oos.styleQuery(ossClient,imgPath,type );

            oos.shutDownClient(ossClient);
            return  url;
        }else
            return  imgPath;
    }




    /**
     * 作品页面直接生产订单
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/createArtOrder")
    public String createArtOrder(@RequestParam Map<String,String> map, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openId = map.get("openId").trim();
            if(openId!=null && !"".equals(openId)){
                String artcode =  map.get("artCode").trim();
                String price =  map.get("price").trim();
                TBizOrder order = new TBizOrder();
//                String orderId = "O" + SeqFactory.getNewSequenceAlone();
                order.setType("1");
                order.setPrice(price);
                order.setUserid(openId);
                //获取用户默认的地址
                WxUserAddress wua =wxUserAddressService.findByuserId(openId);
                if(wua!=null&&wua.getId()!=null)
                    order.setAddress(wua.getId().toString());
                order.setJzstate("1");
                order.setSjprice(price);
                order.setOrderstate("1");
                if(tBizOrderService.saveInfo(order,artcode)){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data",order.getOrderid());
                }else{
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","操作失败");
                }

            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     *
     * @param aucStatus 拍卖会状态
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionList")
    public String auctionList(@RequestParam("aucStatus") String aucStatus,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            TBizAuction auc = new TBizAuction();
            auc.setStatetime(DateUtil.formatDate(new Date()));
            PageRequest page = new PageRequest();
            page.setPageNum(pageNum);
            page.setPageSize(pageSize);
            PageResult result = tBizAuctionService.appaucList(auc,page);
            List<TBizAuction> list = (List<TBizAuction>) result.getContent();
            //获取拍卖会的围观人数
            if(list!=null && list.size()>0){
                AcutionCache ac  = new AcutionCache();
                for (TBizAuction a :list){
                    String key =a.getAuctionid();
                    if(ac.anumberMap.containsKey(key)){
                        Integer number = ac.getAnumberMap(key);
                        a.setSeeNumber(number.toString());
                    }else{
                        int max=300;
                        int min=100;
                        Random random = new Random();
                        int s = random.nextInt(max)%(max-min+1) + min;
                        a.setSeeNumber(String.valueOf(s));
                    }
                }
                result.setContent(list);
            }
            jsonObject.put("oper_res",true);
            jsonObject.put("data",result);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 拍卖会详情
     * @param auctionId
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionDesc")
    public String auctionDesc(@RequestParam("auctionId") String auctionId,
                              @RequestParam(value = "openid",required = false) String openid,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            TBizAuction auc = tBizAuctionService.findByaid(auctionId);
            PageRequest page = new PageRequest();
            page.setPageNum(pageNum);
            page.setPageSize(pageSize);
            jsonObject.put("auc",auc);
            //增加该拍卖会的总人数围观人数
            AcutionCache ac  = new AcutionCache();
            Map<String,Integer> map = ac.anumberMap;
            if(map.containsKey(auctionId)){
                Integer number = map.get(auctionId)+1;
                ac.setAnumberMap(auctionId,number);
            }else
                ac.setAnumberMap(auctionId,1);
            PageResult result = tBizArtService.findByPage(auc.getAuctionid(),page);
            if(wxUserInfoService.wUserIsLog(openid)){
                //判断是否交了保证金
                jsonObject.put("isbzj",tBizOrderService.queryPay(openid,auctionId));
            }else
                jsonObject.put("isbzj",false);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",result);
            jsonObject.put("seeNumber",ac.getAnumberMap(auctionId));
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 拍卖作品的详情
     * @param artid
     * @param openid
     * @param auctionId
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/artDesc")
    public String artDesc(@RequestParam("artid") Integer artid,
                          @RequestParam(value = "openid",required = false) String openid,
                          @RequestParam(value = "auctionId",required = false) String auctionId,
                              HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            HashMap<String,Object> map = tBizArtService.findAllById(artid);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",map);
            if(wxUserInfoService.wUserIsLog(openid)){
                //判断是否交了保证金
                jsonObject.put("isbzj",tBizOrderService.queryPay(openid,auctionId));
            }else
                jsonObject.put("isbzj",false);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/carouseList")
    public  String carouseList(@RequestParam("ends") Integer ends,
                               HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            List<TBizCarouse> list = tBizCarouseService.findInfo(ends);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }


    /**
     * 轮播新闻明细
     * @param cId
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/carouseDesc")
    public  String carouseDesc(@RequestParam("cId") Integer cId,
                               HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            TBizCarouse ca = tBizCarouseService.findById(cId);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",ca);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 创建订单
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/creatOrder")
    public String creatOrder(@RequestParam Map<String,String> map,HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openid =map.get("openid");
            map.remove("openid");
            if(wxUserInfoService.wUserIsLog(openid)){
                TBizOrder order = new TBizOrder();
                String type = map.get("type");
                String price = map.get("price");
//                String address = map.get("address");
                String auctionId = map.get("auctionId");//拍卖会ID
                String artcode = map.get("artCode");//拍卖作品ID
                order.setType(type);
                order.setPrice(price);
                order.setUserid(openid);
                order.setJzstate("1");
                order.setOrderstate("1");
                order.setSjprice(price);
                String orderId = "O" + SeqFactory.getNewSequenceAlone();
                order.setOrderid(orderId);
                TBizOrderArt oa = new TBizOrderArt();
                oa.setArtcode(artcode);
                oa.setOrderid(orderId);
                oa.setAcutionId(auctionId);
                oa.setNumber(1);
                if (tBizOrderService.createOrder(order, oa, request)) {
                    jsonObject.put("oper_res", true);
                    jsonObject.put("data", orderId);
                } else {
                    jsonObject.put("oper_res", false);
                    jsonObject.put("data", "创建订单失败");
                }
            }else {
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 创建购物车订单
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/creatShoppingOrder")
    public String creatShoppingOrder(@RequestParam Map<String,String> map,HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openid =map.get("openid");
            map.remove("openid");
            if(wxUserInfoService.wUserIsLog(openid)){
                String shopId = map.get("shopId").trim();
                List<String> shopIds = new ArrayList<>();
                if(shopId.contains(",")){
                    String[] ss = shopId.split(",");
                    for(String i : ss){
                        shopIds.add(i);
                    }
                }else{
                    shopIds.add(shopId);
                }
                //生成购物车订单
                TBizOrder order = new TBizOrder();
                String orderId = "O" + SeqFactory.getNewSequenceAlone();
                //判断是一件物品还是多件物品
                String price = map.get("price");
//                String address = map.get("address");
                String artcode = map.get("artCode");//作品ID
                List<TBizOrderArt> oaList = new ArrayList<>();
                Double allPrice = 0.00;
                if(price.contains(",")&&artcode.contains(",")){
                    String[] prices = price.split(",");
                    String[] artcodes = artcode.split(",");
                    for(int i =0;i<prices.length;i++){
                        TBizOrderArt oa = new TBizOrderArt();
                        oa.setArtcode(artcodes[i]);
                        oa.setOrderid(orderId);
                        oa.setAcutionId(null);
                        oa.setNumber(1);
                        oaList.add(oa);
                        allPrice=allPrice+Double.valueOf(prices[i]);
                    }
                }else{
                    allPrice =Double.valueOf(price);
                    TBizOrderArt oa = new TBizOrderArt();
                    oa.setArtcode(artcode);
                    oa.setOrderid(orderId);
                    oa.setAcutionId(null);
                    oa.setNumber(1);
                    oaList.add(oa);
                }
                order.setType("1");
                order.setPrice(allPrice.toString());
                order.setUserid(openid);
                //获取用户默认的地址
                WxUserAddress wua =wxUserAddressService.findByuserId(openid);
                if(wua!=null&&wua.getId()!=null)
                    order.setAddress(wua.getId().toString());
                order.setJzstate("1");
                order.setSjprice(allPrice.toString());
                order.setOrderid(orderId);
                order.setOrderstate("1");
                if (tBizOrderService.createShoppingOrder(order, oaList,shopIds, request)) {
                    jsonObject.put("oper_res", true);
                    jsonObject.put("data", orderId);
                } else {
                    jsonObject.put("oper_res", false);
                    jsonObject.put("data", "创建购物车订单失败");
                }

            }else {
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 添加到购物车
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/addShopping")
    public String addShopping(@RequestParam Map<String,String> map,HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openid =map.get("openid");
            if(wxUserInfoService.wUserIsLog(openid)){
                TBizShopping shop =new TBizShopping();
                String artId = map.get("artId");
                shop.setUserId(openid);
                shop.setArtId(artId);
                shop.setCreatTime(DateUtil.formatDate(new Date()));
                System.out.println(shop.toString());
                if(tBizShoppingService.save(shop)){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data","加入购物车成功");
                }else{
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","已被他人添加到购物车");
                }
            }else {
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/queryShopping")
    public String queryShopping(@RequestParam("openid") String openid,
                                @RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize,
                                HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(wxUserInfoService.wUserIsLog(openid)){
                PageRequest pageRequest = new PageRequest();
                pageRequest.setPageSize(pageSize);
                pageRequest.setPageNum(pageNum);
                TBizShopping shop = new TBizShopping();
                shop.setUserId(openid);
                PageResult result = tBizShoppingService.findByPage(shop,pageRequest);
                jsonObject.put("oper_res",true);
                jsonObject.put("data",result);
            }else {
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 购物车删除
     * @param id
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/delShopping")
    public String delShopping(@RequestParam("id") String id, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(tBizShoppingService.delShop(id)){
                jsonObject.put("oper_res",true);
                jsonObject.put("data","删除成功");
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }




//    @RequestMapping(value = "/addShopping")
//    public String addShopping(@RequestParam Map<String,String> map,HttpServletRequest request, HttpSession session){
//        JSONObject jsonObject = new JSONObject();
//        try{
//            String openid =map.get("openid");
//            map.remove("openid");
//            if(wxUserInfoService.wUserIsLog(openid)){
//
//            }else {
//                jsonObject.put("oper_res",false);
//                jsonObject.put("data","请先登录");
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            jsonObject.put("oper_res",false);
//            jsonObject.put("data","请求失败");
//        }
//        return jsonObject.toJSONString();
//    }



    /**
     * 订单列表
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/orderList")
    public  String orderList(@RequestParam("type") String type,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("openid") String openid,
                             HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(wxUserInfoService.wUserIsLog(openid)){
                TBizOrder orderInfo = new TBizOrder();
                orderInfo.setType(type);
                orderInfo.setUserid(openid);
                PageRequest page = new PageRequest();
                page.setPageNum(pageNum);
                page.setPageSize(pageSize);
                PageResult pageResult = tBizOrderService.findByPages(orderInfo,page);
                jsonObject.put("oper_res",true);
                jsonObject.put("data",pageResult);
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }


    /**
     * 获取收获地址列表
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/addressList")
    public  String addressList(@RequestParam("openid") String openid,HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(wxUserInfoService.wUserIsLog(openid)){
                WxUserAddress wua = new WxUserAddress();
                wua.setWxOpenId(openid);
                List<WxUserAddress> list =wxUserAddressService.findByParam(wua);
                jsonObject.put("oper_res",true);
                jsonObject.put("data",list);
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }

    /**
     * 收获地址的修改
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/addressEdit")
    public  String addressEdit(@RequestParam Map<String,String> map,
                             HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openid =map.get("openid");
            map.remove("openid");
            if(wxUserInfoService.wUserIsLog(openid)){
                String type = map.get("type");
                map.remove("type");
                boolean result = true;
                if ("add".equals(type)) {
                    WxUserAddress wua =JSONObject.parseObject(JSONObject.toJSONString(map), WxUserAddress.class);
                    wua.setWxOpenId(openid);
                    result = wxUserAddressService.save(wua);
                } else if ("update".equals(type)){
                    WxUserAddress wua =JSONObject.parseObject(JSONObject.toJSONString(map), WxUserAddress.class);
                    wua.setWxOpenId(openid);
                    result = wxUserAddressService.update(wua);
                }else if("del".equals(type))
                    result = wxUserAddressService.del(Integer.valueOf(map.get("id")));
                if(result){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data","操作成功");
                }else {
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","操作失败");
                }
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }


        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }


    /**
     * 拍卖行出价
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionEdit")
    public  String text(@RequestParam Map<String,String> map,
                        HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openId = map.get("openid");
            if(openId!=null && !"".equals(openId.trim())){
                String key = map.get("auctionid");
                AcutionCache ac = new AcutionCache();
                TBizAuctionRecord ar =JSONObject.parseObject(JSONObject.toJSONString(map), TBizAuctionRecord.class);
                ar.setCreatTime(DateUtil.formatDate(new Date()));
                ar.setUserId(openId);
                if(ac.setCodeMap(key+"_"+ar.getArtCode(),ar)){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data","出价成功");
                }else {
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","你的出价低于最新出价,请重新出价");
                }

            }else {
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","出价失败");
        }
        return  jsonObject.toString();
    }

    /**
     * 出价查询
     * @param auctionid
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionQue")
    public String auctionQue(@RequestParam("auctionid") String auctionid,
                             @RequestParam("artcode") String artcode,
                             @RequestParam(value = "pagesize",required = false) Integer pagesize,
                             HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            AcutionCache ac = new AcutionCache();
            List<TBizAuctionRecord> list = ac.getCodeMap(auctionid+"_"+artcode);
            if(list!=null&&list.size()>0){
                if(pagesize!=null&&pagesize!=0){
                    //获取最新的5条记录
                    if(list.size()>pagesize)
                        list =list.subList(list.size()-pagesize,list.size());
                    for(TBizAuctionRecord ar :list){
                        WxUserInfo info = wxUserInfoService.getById(ar.getUserId());
                        ar.setUserName(info.getNickName());
                        ar.settPic(info.getAvatarUrl());
                    }
                }
            }
            jsonObject.put("oper_res",true);
            jsonObject.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","出价失败");
        }
        return  jsonObject.toString();
    }


    /**
     * 拍卖作品结束
     * @param auctionid
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionEnd")
    public String auctionEnd(@RequestParam("auctionid") String auctionid,
                             @RequestParam("artcode") String artcode,
                             @RequestParam("auctionName") String auctionName,
                             HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            System.out.println("***********从页面进入拍卖作品结束*************");
            AcutionCache ac = new AcutionCache();
            String key = auctionid+"_"+artcode;
            System.out.println("***************key*************");
            System.out.println(key);
            List<TBizAuctionRecord> list = ac.getCodeMap(key);
            if(arService.saveAll(list)){
                System.out.println("*********出价记录保持*****");
                arService.recodeSave(list);
                ac.remove(key,auctionName);
                jsonObject.put("oper_res",true);
                jsonObject.put("data",list);
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","出价失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","出价失败");
        }
        return  jsonObject.toString();
    }

    /**
     * 拍卖会结束
     * @param auctionid
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionAllEnd")
    public String auctionAllEnd(@RequestParam("auctionid") String auctionid,
                             HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
//            AcutionCache ac = new AcutionCache();
//            List<TBizAuctionRecord> list = ac.getCodeMap(auctionid+"_"+artcode);
//            if(arService.saveAll(list)){
//                ac.remove(auctionid,artcode);
//                jsonObject.put("oper_res",true);
//                jsonObject.put("data",list);
//            }else{
//                jsonObject.put("oper_res",false);
//                jsonObject.put("data","出价失败");
//            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","出价失败");
        }
        return  jsonObject.toString();
    }


    /**
     * 根据分类获取作品列表
     * @param style
     * @param style1
     * @param pageNum
     * @param pageSize
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/artList")
    public String artList(@RequestParam(value = "style",required=false) String style,
                          @RequestParam(value = "style1",required=false) String style1,
                          @RequestParam(value = "artName",required=false) String artName,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize,
                          HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            PageRequest page = new PageRequest();
            page.setPageNum(pageNum);
            page.setPageSize(pageSize);
            TBizArt art = new TBizArt();
            if(artName!=null && !"".equals(artName))
                art.setName("%"+artName+"%");
            art.setStyle(style);
//            art.setStyle(style1);
            art.setSalestate("0");
            System.out.println(art.getStyle());
            PageResult pageResult = tBizArtService.findNoauc(art,page);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","获取失败");
        }
        return  jsonObject.toString();
    }

    /**
     * 作品详情
     * @param aId
     * @return
     */
    @RequestMapping(value = "/artInfoDesc")
    public String artInfoDesc(@RequestParam("aId") Integer aId){
        JSONObject jsonObject = new JSONObject();
        try{
            TBizArt art = tBizArtService.findById(aId);
            jsonObject.put("oper_res",true);
            jsonObject.put("data",art);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","出价失败");
        }
        return  jsonObject.toString();
    }

    /**
     * 字典查询
     * @param level
     * @param fId
     * @param sName
     * @return
     */
    @RequestMapping(value = "/styleQuery")
    private String styleQuery(@RequestParam("level") Integer level
                             ,@RequestParam(name = "fId",required = false) Integer fId
                             ,@RequestParam(name = "sName",required = false) String sName){
        JSONObject jsonObject = new JSONObject();
        try{
            if(level==1&&(sName!=null&&!"".equals(sName))){
                List<Object> styleList =CodeCache.levelOneMap.get(sName);
                jsonObject.put("oper_res",true);
                jsonObject.put("data",styleList);
            }
//            else{
//                List<Object> c2 = CodeCache.levelTwoMap.get(fId.toString());
//                jsonObject.put("oper_res",true);
//                jsonObject.put("data",c2);
//            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","查询失败");
        }
        return  jsonObject.toString();
    }

    /**
     * 支付页面的订单详情
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "orderDesc")
    public  String orderDesc(@RequestParam Map<String,String> map,
                             HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openId = map.get("openId").trim();
            if(openId!=null &&!"".equals(openId)){
                String type = map.get("type").trim();;
                String orderId = map.get("orderId").trim();;
                TBizOrder order = new TBizOrder();
                order.setOrderid(orderId);
                order.setType(type);
                order.setUserid(openId);
                TBizOrderArt oa  =tBizOrderService.findInfoByOrderID(order);
                jsonObject.put("oper_res",true);
                jsonObject.put("data",oa);
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }


    /**
     * 支付页面编辑地址
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/editOrderAddress")
    public  String editOrderAddress(@RequestParam Map<String,String> map,
                        HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openId = map.get("openId").trim();
            if(openId!=null &&!"".equals(openId)){
                String addressId = map.get("addressId").trim();
                String orderId = map.get("orderId").trim();
                TBizOrder order = new TBizOrder();
                order.setOrderid(orderId);
                order.setAddress(addressId);
                if(tBizOrderService.Update(order)){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data","操作成功");
                }else{
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","操作失败");
                }
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }


    /**
     * 开拍预约
     * @param map
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/addRemind")
    public  String addRemind(@RequestParam Map<String,String> map
                            ,HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            String openId = map.get("openId").trim();
            if(openId!=null &&!"".equals(openId)){
                TBizAuctionRemind ar = new TBizAuctionRemind();
                ar.setUserId(openId);
                ar.setAuctionId(map.get("auctionId").trim());
                if(tBizAuctionRemindService.save(ar)){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data","预约成功");
                }else{
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","预约失败");
                }
            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","请先登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
            jsonObject.put("data","请求失败");
        }
        return jsonObject.toJSONString();
    }



//    @RequestMapping(value = "/text")
//    public  String text(HttpServletRequest request, HttpSession session){
//        JSONObject jsonObject = new JSONObject();
//        try{
//            TBizOrder order = new TBizOrder();
//            order.setOrderid("O20052017200128510016");
//            order.setOrderstate("2");
//            order.setTicketid("ssafgasgasg");
//            order.setPaytype("1");
//            WxPayRecord wpr = new WxPayRecord();
//            wpr.setPrice("100.00");
//            wpr.setPayType("wx");
//            wpr.setOrderId("O20052017200128510016");
//            wpr.setTicketId("ssafgasgasg");
//            wpr.setPayTime(DateUtil.formatDate(new Date()));
//            boolean a = tBizOrderService.payTrue(order,wpr);
//            System.out.println(a);
//            //支付记录
//            jsonObject.put("oper_res",a);
//            jsonObject.put("data","操作成功");
//        }catch (Exception e){
//            e.printStackTrace();
//            jsonObject.put("oper_res",false);
//            jsonObject.put("data","请求失败");
//        }
//        return jsonObject.toJSONString();
//    }

}
